package com.unziper.helpapp.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class UploadManager implements IUploadManager {

	private List<String> zipFileNames;

	public UploadManager() {
		initialize();
	}

	private void initialize() {
		zipFileNames = new ArrayList<String>();
	}

	public void saveToFile(InputStream inputStream, String fileName) {
		File outFile = new File(OUTPUT_DIRECTORY);
		if (!outFile.exists()) {
			outFile.mkdir();
		}

		File newFile = new File(OUTPUT_DIRECTORY + File.separator + fileName);
		try {
			newFile.createNewFile();
			FileOutputStream outputStream = new FileOutputStream(newFile);

			byte[] buffer = new byte[4096];
			int lenght = 0;
			while ((lenght = inputStream.read(buffer)) > 0) {
				outputStream.write(buffer, 0, lenght);
			}
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		getZipFileNames().add(fileName);
	}

	public List<String> getZipFileNames() {
		return zipFileNames;
	}

	public void setZipFileNames(List<String> zipFileNames) {
		this.zipFileNames = zipFileNames;
	}

}
